package com.foo.designpattern.iterator;

import java.util.ArrayList;
import java.util.List;

/**
 * com.foo.designpattern.iterator
 * Created with IntelliJ IDEA.
 * Created by jiang on 2016-05-31 11:20.
 *
 * @Description 具体迭代器(ConcreteIterator)角色
 * 具体迭代器角色：实现抽象迭代器类(Iterator)定义的接口，并保持迭代过程中的游标位置。
 */

public class ConcreteIterator implements Iterator {
    // 定义一个具体的聚集对象
    private ConcreteAggregate aggregate;
    private int current = 0;

    // 初始化时将具体的聚集对象传入
    public ConcreteIterator(ConcreteAggregate aggregate) {
        this.aggregate = aggregate;
    }

    @Override
    public Object first() {
        // 得到聚集的第一个对象
        return aggregate.getItems().get(0);
    }

    @Override
    public Object next() {
        // 得到聚集的下一个对象
        Object obj = null;
        current++;
        if (current < aggregate.count()) {
            obj = aggregate.getItems().get(current);
        }
        return obj;
    }

    @Override
    public Object currentItem() {
        // 返回当前的聚集对象
        return aggregate.getItems().get(current);
    }

    @Override
    public boolean hasNext() {
        // 判断当前是否还有下一个对象
        if (current < aggregate.count()) {
            return true;
        }
        return false;
    }
}
